const config = require('../../../common/config');
const util = require('../../../utils/util');
var fileSelectIndex = 0;
var shopFileStatus = [];
var shopId = '';

Page({
    data: {
        shopConsumeMoneyAvg:'',
        parkCodes:['免费停车','收费停车','无'],
        parkCodeIndex:2,
        wifiCodes:['免费Wi-Fi','收费Wi-Fi','无'],
        wifiCodeIndex:2,
        shopFiles:[],
        shopFileStatus:[],
        shopFileFolderId:'',
        uploadedShopFileLength:0,
        shopMobile:'',
        startTime:'',
        endTime:'',
        otherService:'',
        otherServiceLength:0,
        shopLinkMan:'',
        shopDuty:'',
        isSubmit:true,
        isLoading:false,
        userId:''

    },
    onLoad:function (opt) {
        wx.setNavigationBarTitle({
            title: '门店发布'
        })
        shopId = opt.shopId;
        util.debug("storepulishstep3js保存成功，门店ID:"+shopId);
        this.setData({
            userId: wx.getStorageSync(config.USER_ID)
        })
    },
    chooseShopFileImage: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            count:1,
            success: function (res) {
                fileSelectIndex++;
                that.setData({
                    shopFiles: that.data.shopFiles.concat(res.tempFilePaths),
                    shopFileStatus:that.data.shopFileStatus.concat(0)
                });

                const uploadTask = wx.uploadFile({
                    header: {
                        "Content-Type": "multipart/form-data"
                    },
                    url: config.COMMPT_REQPATH + '/uploadFiles',
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    formData: {
                        accessToken: '',
                        refreshToken: '',
                        serviceName: 'wxShopManageService',
                        methodName: 'uploadShopImg',
                        reqData: JSON.stringify({userId: that.data.userId,folderId:that.data.shopFileFolderId})
                    },
                    success: function (res) {
                        var data = JSON.parse(res.data);
                        util.debug("storepushstep3js文件上传结果：");
                        util.debug(data);

                        shopFileStatus = that.data.shopFileStatus;
                        if (data.code != 1) {
                            shopFileStatus[fileSelectIndex-1] = 2 ;
                        } else {
                            shopFileStatus[fileSelectIndex-1] = 1 ;
                            that.setData({
                                uploadedShopFileLength : fileSelectIndex,
                                shopFileFolderId:data.data.folderId
                            })
                        }
                    },
                    fail: function (err) {
                        uploadTask.abort();
                        shopFileStatus[fileSelectIndex-1] = 2 ;
                    },
                    complete:function () {
                        that.setData({
                            shopFileStatus:shopFileStatus
                        })
                    }
                    
                })

                uploadTask.onProgressUpdate((res) => {
                    util.debug('storepushstep3js上传进度:'+ res.progress)
                    util.debug('storepushstep3js已经上传的数据长度:'+res.totalBytesSent)
                    util.debug('storepushstep3js预期需要上传的数据总长度:'+ res.totalBytesExpectedToSend)
                })

            }
        })
    },
    previewShopFileImage: function(e){
        wx.previewImage({
            current: e.currentTarget.id,
            urls: this.data.shopFiles
        })
    },
    bindStartTimeChange:function (e) {
        this.setData({
            startTime:e.detail.value
        })
    },
    bindEndTimeChange:function (e) {
        this.setData({
            endTime:e.detail.value
        })
    },
    bindSubmit:function (e) {
        var _this = this;
        var shopConsumeMoneyAvg = this.data.shopConsumeMoneyAvg;
        var startTime = this.data.startTime;
        var endTime = this.data.endTime;
        var shopPark = this.data.parkCodes[this.data.parkCodeIndex];
        var shopWifi = this.data.wifiCodes[this.data.wifiCodeIndex];
        var otherService = this.data.otherService;
        var shopLinkMan = this.data.shopLinkMan;
        var shopMobile = this.data.shopMobile;
        var shopDuty = this.data.shopDuty;

        wx.showLoading({
            title: '提交中..',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask:true
        })

        _this.setData({
            isLoading: false
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'wxShopManageService',
                methodName: 'saveShopInfo',
                reqData:JSON.stringify({
                    shopConsumption:shopConsumeMoneyAvg,
                    shopStartTime:startTime,
                    shopEndTime:endTime,
                    shopPark:shopPark,
                    shopWifi:shopWifi,
                    shopLinkMan:shopLinkMan,
                    shopOtherService:otherService,
                    shopMobile:shopMobile,
                    shopLinkManDuty:shopDuty,
                    shopId:shopId,
                    shopFileFolderId:_this.data.shopFileFolderId
                })
            },
            success: function (res) {
                var resultData = res.data;
                wx.hideLoading();
                _this.setData({
                    isLoading: false
                })
                if(resultData.code == 1){
                    // wx.redirectTo({
                    //     url: '../../../pages/mystore/index'
                    // })
                    wx.navigateBack({
                        delta: 3
                    })
                }else{
                    util.debug('storepulishstep3保存店铺信息失败：');
                    util.debug(resultData);
                }
            },
            fail:function (e) {
                wx.hideLoading();
                _this.setData({
                    isLoading: false
                })
                util.debug(e.errMsg);
            }
        })


    },
    shopConsumeMoneyAvgChange:function (e) {
        this.setData({
            shopConsumeMoneyAvg:e.detail.value
        })
    },
    bindParkChange:function (e) {
        this.setData({
            parkCodeIndex:e.detail.value
        })
    },
    shopMobileChange:function (e) {
        this.setData({
            shopMobile:e.detail.value
        })
        if(this.data.shopMobile !=''){
            this.setData({
                isShopMobileWarn:false
            })
        }
    },
    bindWifiChange:function (e) {
        this.setData({
            wifiCodeIndex:e.detail.value
        })
    },
    shopLinkManChange:function (e) {
        this.setData({
            shopLinkMan:e.detail.value
        })
    },
    shopDutyChange:function (e) {
        this.setData({
            shopDuty:e.detail.value
        })
    },
    otherServiceChange:function (e) {
        var otherServiceContent = e.detail.value;
        if(otherServiceContent.length <=100){
            this.setData({
                otherServiceLength:otherServiceContent.length,
                otherService:otherServiceContent
            })
        }
    }
})